<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: Menu.php 360 2011-10-17 03:32:52Z beimuaihui@gmail.com $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class Menu  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'menu';
		$this->myPrimary=$this->_primary= 'menu_id';
		parent::__construct();
	}

	function getSame($arr) {	
		$ret=array();
		
		$arr=array("menu_name"=>$arr['menu_name']);
		return parent::getSame($arr);	   
	
	}
	

    function updatePids()
    {
    	$pid=0;$pid_value=',';$pid_col='menu_pid';
    	$this->_db->query("UPDATE {$this->_name} SET {$pid_col}s=''");
    	$this->_db->query("UPDATE {$this->_name} SET {$pid_col}s=concat('{$pid_value}',{$this->myPrimary},',') WHERE {$pid_col}={$pid}");
    	while($this->_db->query("UPDATE {$this->_name} a
					  JOIN {$this->_name} b
					    ON a.{$pid_col} = b.{$this->myPrimary}
					SET a.{$pid_col}s = CONCAT(b.{$pid_col}s,a.{$this->myPrimary},',')
					WHERE b.{$pid_col}s != '' and a.{$pid_col}s=''")->rowCount()){
		}
		
		return true;
    }
    
    function getByKey($key=''){
    	$rs_menu=$this->getList(array("key_name"=>$key));
		$menu=@$rs_menu[0];
		$menu["id"]='menu_'.@$menu['menu_id'];
		$menu['text']=@$menu['menu_name'];
		$menu["icon"]=BAOGG_UPLOAD_URL."image/menu/".@$menu['img_icon'];			
		$menu["url"]=BAOGG_BASE_URL.@$menu['url'];
		return $menu;		
    }
    
    function getByUrl($url=''){
    	$rs_menu=$this->getList(array("url"=>$url.'%'));
		$menu=@$rs_menu[0];
		$menu["id"]='menu_'.@$menu['menu_id'];
		$menu['text']=$menu['menu_name'];
		$menu["icon"]=BAOGG_UPLOAD_URL."image/menu/".@$menu['img_icon'];			
		$menu["url"]=BAOGG_BASE_URL.@$menu['url'];
		return $menu;		
    }
    
    function getReportMenu(){
    	$Permission=new Permission();
    	$User=new User();
    	
        $arr_menu_id=$Permission->checkResource('menu');
        $rs_menu_pid=$this->getList(array("key_name"=>"edu"));
        $rs_menu_report=(array)$this->getList(array("menu_pid"=>(int)($rs_menu_pid[0]['menu_id'])),array("sort"=>"ordering","dir"=>"asc"));
        foreach($rs_menu_report as $k=>$v){
        	if(!isset($arr_menu_id[$v['menu_id']]) && !$User->isAdmin()){
        		unset($rs_menu_report[$k]);
        	}
        }
        return array_values((array)$rs_menu_report);
    }
}
